package com.epoint.mobileoa.ui.contacts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.SearchContactsByPinyinTask;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.MobileOperation;
import com.epoint.mobileoa.tools.ToastHelp;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @author liyc
 * @time 2012-1-9 下午6:23:28
 * @annotation 
 */
public class ContactsView extends SuperView {

	private ArrayList<String> backflaglist;// 用作返回按钮作标记
	private List<Map<String, Object>> mData;
	private ListView lv;
	private static int dispalyPersonChoose = 2;// 1代表显示人员选择，2代表不显示人员选择
	private EditText et_cz;
	private Button bt_cz;
	MyAdapter adapter;
	public static final int SearchContactsByPinyin = 1;

	@Override
	public void initControls() {


		lv = (ListView) findViewById(R.id.lvContacts2);
		et_cz = (EditText) findViewById(R.id.et_cz);
		bt_cz = (Button) findViewById(R.id.bt_cz);

		backflaglist = new ArrayList<String>();

		mData = getData(getString(R.string.contactsRootDeptGuid));

		adapter = new MyAdapter(this);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		lv.setOnItemLongClickListener(this);

		top_left_bt.setOnClickListener(this);

		bt_cz.setOnClickListener(this);

		if(DBHelp.getConfigValue(this, ConfigKey.isTuojiLogin).equals("1")){
			DBHelp.setConfigValue(this, ConfigKey.autologin, "0");
		}

		et_cz.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				top_pb.setVisibility(View.VISIBLE);
				HashMap param=new HashMap();
				param.put("tj", s.toString());
				Task task = new SearchContactsByPinyinTask(ContactsView.this,param);
				MainService.addTask(task);
			}    
		});
	}

	private List<Map<String, Object>> getData(String guid) {
		List<Map<String, Object>> list = DBHelp.getRootDeptMapReturn(this, 0, guid);
		return list;
	}

	public final class ViewHolder {
		public ImageView img;
		public TextView title;
		public TextView info;
		public CheckBox cbPerson;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		ViewHolder holder = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.listviewwithbtnandimg,
						null);
				holder.img = (ImageView) convertView.findViewById(R.id.img);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.info = (TextView) convertView.findViewById(R.id.info);
				holder.cbPerson = (CheckBox) convertView
						.findViewById(R.id.cbPerson);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if(mData.get(position).get("info").equals("部门")){
				holder.cbPerson.setVisibility(View.INVISIBLE);

				if(getBackgroundStr().equals("background")){
					holder.title.setTextColor(getResources().getColor(com.epoint.mobileoa.R.color.white));
				}
				else{
					holder.title.setTextColor(getResources().getColor(com.epoint.mobileoa.R.color.black));
				}
				holder.img.setBackgroundResource(R.drawable.contacts_ou);
			}
			else{
				holder.img.setBackgroundResource(R.drawable.contackt_user);
				if(getBackgroundStr().equals("background")){
					holder.title.setTextColor(getResources().getColor(com.epoint.mobileoa.R.color.solid_yellow));
				}
				else{
					holder.title.setTextColor(getResources().getColor(com.epoint.mobileoa.R.color.blue));
				}
			}

			holder.title.setText((String) mData.get(position).get("title"));
			holder.info.setText(Html.fromHtml("<font color='orange'>"+(String) mData.get(position).get("dp")+"</font>"));
			if (dispalyPersonChoose != 1) {
				holder.cbPerson.setVisibility(View.INVISIBLE);
			}
			return convertView;
		}

	}

	public void backLogic() {
		/**
		 * 返回按钮操作逻辑 liyc 2011.08.02
		 */
		if (backflaglist.size() == 0) {
			finish();
		} else if (backflaglist.size() == 1) {
			mData = getData(getString(R.string.contactsRootDeptGuid));
			adapter.notifyDataSetChanged();
		} else {
			mData = getData(backflaglist.get(backflaglist.size() - 2));
			adapter.notifyDataSetChanged();
		}

		if (backflaglist.size() > 0) {
			backflaglist.remove(backflaglist.size() - 1);
		}
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			mData = (List<Map<String, Object>>) param[1];
			adapter.notifyDataSetChanged();
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.personchoose2;
	}


	@Override
	public void KEYCODE_BACK() {
		backLogic();
	}

	@Override
	public String setTitle() {
		return getString(R.string.txl);
	}

	@Override
	public void onClick(View v) {
		if(top_left_bt.getId()==v.getId()){
			backLogic();
		}
		else if(bt_cz.getId()==v.getId()){
			String tj = et_cz.getText().toString().trim().toLowerCase();// 查询条件
			if(tj.length()!=0){
				top_pb.setVisibility(View.VISIBLE);
				HashMap param=new HashMap();
				param.put("tj", tj);
				Task task = new SearchContactsByPinyinTask(this,param);
				MainService.addTask(task);
				InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(et_cz.getWindowToken(), 0);
			}
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(parent.getId()==lv.getId()){
			if (mData.get(position).get("info").equals("人员")) {
				String mobile = mData.get(position).get("mobile").toString();
				if(mobile.equals("")){
					ToastHelp.showToast(this, "没有手机号码！", 1000);
				}
				else{
					MobileOperation.Call(this, mobile);
				}
			}
		}
		return super.onItemLongClick(parent, view, position, id);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(parent.getId()==lv.getId()){

			if (mData.get(position).get("info").equals("部门")) {
				String deptTitle = mData.get(position).get("title").toString();
				backflaglist.add(mData.get(position).get("guid").toString());
				System.out.println(mData.get(position).get("guid").toString());
				mData = getData(mData.get(position).get("guid").toString());
				adapter.notifyDataSetChanged();
			} else {
				// 点击人员后操作
				if (dispalyPersonChoose == 2) {
					Intent intent = new Intent(this,
							ContactsDetailView.class);
					intent.putExtra("userguid", mData.get(position).get(
							"guid").toString());
					startActivity(intent);
				}
			}

		}
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}



}
